import Vue from "vue";
import VueRouter from "vue-router";
//用户
import Login from "../pages/Login";
import Register from "../pages/Register";
import Home from "../pages/Home";
import Person from "../pages/Home/Person";
import Competition from "../pages/Home/Competition";
import competition from "../pages/Home/Competition/competition";
import competitionDetail from "../pages/Home/Competition/competitionDetail";
import player from "../pages/Home/Competition/Player";
import privateMessage from "../pages/Home/PrivateMessage";

import Ask from "../pages/Home/Ask";
import homepage from "../pages/Home/index/index.vue";

//管理员
import Manage from "../pages/Manage";
import managehomepage from "../pages/Manage/index/index.vue";
import managePerson from "../pages/Manage/Person";
import manageCompetition from "../pages/Manage/Competition";
import managecompetition from "../pages/Manage/Competition/competition";
import managecompetitionDetail from "../pages/Manage/Competition/competitionDetail";
import manageplayer from "../pages/Manage/Competition/Player";
import managePrivateMessage from "../pages/Manage/PrivateMessage";
import Manager from "../pages/Manage/manage";
import ManagerCompetitions from "../pages/Manage/manage/competitions";
// import ManagerUsers from "../pages/Manage/manage/user";
import manageAsk from "../pages/Manage/Ask";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "login",
    component: Login,
  },
  // {
  //   path: "/",
  //   name: "home",
  //   component: Home,
  // },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: "/register",
    name: "register",
    component: Register,
  },
  {
    path: "/home",
    name: "home",
    component: Home,
    redirect: "/home/index",
    meta: { requiresAuth: true, title: "首页" }, // 标记该路由需要登录
    children: [
      {
        path: "/home/index",
        name: "homepage", // 添加了name属性
        component: homepage,
        meta: {
          requiresAuth: true,
          title: "首页",
        },
      },
      {
        // path: "/home/person/:id",
        path: "/home/person",
        name: "person", // 添加了name属性
        component: Person,
        meta: {
          requiresAuth: true,
          title: "个人详情",
        },
      },
      {
        path: "/home/competition",
        name: "Competition", // 添加了name属性
        component: Competition,
        // props: true ,// 自动将 params 转为组件 props
        meta: {
          requiresAuth: true,
          title: "全部比赛",
        },
        redirect: "/home/competition/index",
        children: [
          {
            path: "/home/competition/index",
            name: "competition",
            component: competition,
            meta: {
              requiresAuth: true,
              title: "比赛查询",
            },
          },
          {
            path: "/home/competition/:id",
            name: "competitionDetail", // 添加了name属性
            component: competitionDetail,
            props: true, // 自动将 params 转为组件 props
            meta: {
              requiresAuth: true,
              title: "比赛详情",
            },
          },
          {
            path: "/home/competition/:competitionId/player/:userId",
            name: "player",
            component: player,
            meta: {
              requiresAuth: true,
              title: "选手信息",
            },
          },
        ],
      },
      {
        path: "/home/ask",
        name: "ask", // 添加了name属性
        meta: {
          requiresAuth: true,
          title: "AI 问答",
        },
        component: Ask,
      },
      {
        path: "/home/privateMessage",
        name: "privateMessage",
        component: privateMessage,
        meta: {
          requiresAuth: true,
          title: "私信",
        },
      },
    ],
  },
  {
    path: "/manage",
    name: "manage",
    component: Manage,
    redirect: "/manage/index",
    meta: { requiresAuth: true, title: "首页" }, // 标记该路由需要登录
    children: [
      {
        path: "/manage/index",
        name: "manageHomepage", // 添加了name属性
        component: managehomepage,
        meta: {
          requiresAuth: true,
          title: "首页",
        },
      },
      {
        // path: "/home/person/:id",
        path: "/manage/person",
        name: "managePerson", // 添加了name属性
        component: managePerson,
        meta: {
          requiresAuth: true,
          title: "个人详情",
        },
      },
      {
        path: "/manage/competition",
        name: "manageCompetition", // 添加了name属性
        component: manageCompetition,
        // props: true ,// 自动将 params 转为组件 props
        meta: {
          requiresAuth: true,
          title: "全部比赛",
        },
        redirect: "/manage/competition/index",
        children: [
          {
            path: "/manage/competition/index",
            name: "managecompetition",
            component: managecompetition,
            meta: {
              requiresAuth: true,
              title: "比赛查询",
            },
          },
          {
            path: "/manage/competition/:id",
            name: "managecompetitionDetail", // 添加了name属性
            component: managecompetitionDetail,
            props: true, // 自动将 params 转为组件 props
            meta: {
              requiresAuth: true,
              title: "比赛详情",
            },
          },
          {
            path: "/manage/competition/:competitionId/player/:userId",
            name: "manageplayer",
            component: manageplayer,
            meta: {
              requiresAuth: true,
              title: "选手信息",
            },
          },
        ],
      },
      {
        path: "/manage/manager",
        name: "manager",
        component: Manager,
        meta: {
          requiresAuth: true,
          title: "管理",
        },
        children: [
          {
            path: "/manage/manager/competition",
            name: "managerCompetitions", // 添加了name属性
            component: ManagerCompetitions,
            meta: {
              requiresAuth: true,
              title: "比赛管理",
            },
          },
          // {
          //   path: "/manage/manager/users",
          //   name: "managerUsers", // 添加了name属性
          //   component: ManagerUsers,
          //   meta: {
          //     requiresAuth: true,
          //     title: "用户管理",
          //   },
          // },
        ],
      },
      {
        path: "/manage/ask",
        name: "manageAsk", // 添加了name属性
        meta: {
          requiresAuth: true,
          title: "AI 问答",
        },
        component: manageAsk,
      },
      {
        path: "/manage/privateMessage",
        name: "managePrivateMessage",
        component: managePrivateMessage,
        meta: {
          requiresAuth: true,
          title: "私信",
        },
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history", // 取消注释以启用 history 模式
  // base: process.env.BASE_URL, // 根据需要启用
  routes,
});

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 可选：同时重写 replace 方法
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch((err) => err);
};

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem("token");
  const requiresAuth = to.matched.some((record) => record.meta.requiresAuth);
  if (requiresAuth) {
    if (!token) {
      // 避免重复重定向到登录页
      if (to.path !== "/login") {
        next({
          path: "/login",
          query: { redirect: to.fullPath },
        });
      } else {
        next(); // 已经是登录页则放行
      }
    } else {
      next(); // 已认证用户放行
    }
  } else {
    // 不需要认证的路由直接放行
    next();
  }
});

export default router;
